const path = require("path");
const webpack = require("webpack");
const VueLoaderPlugin = require("vue-loader/lib/plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");

// 1. 编写入口文件
module.exports = {
  entry: "./src/main.js",
  output: {
    path: path.join(__dirname, "dist"),
    filename: "[name]-[contenthash:8].bundle.js",
  },
  // 2. 安装 vue-loader
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: "vue-loader",
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: "babel-loader",
      },
      {
        test: /\.(js|vue)$/,
        exclude: /node_modules/,
        use: ["eslint-loader"],
        enforce: "pre",
      },
      {
        test: /\.less$/,
        use: ["style-loader", "css-loader", "less-loader"],
      },
      // css 不能和 less 一起打包
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"],
      },
      {
        test: /.png$/,
        use: {
          loader: "url-loader",
          options: {
            // 超出 10kb 的文件采用 file-loader 打包
            // 小于 10kb 的转换成 base64 嵌入代码中
            limit: 10 * 1024, // 10 KB
            // 不使用 ESM 的异步加载,否则会出现 src 为 [object Module] 的问题
            esModule: false,
          },
        },
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      // 4. 配置 html: title
      title: "vue",
      template: "./public/index.html",
    }),
    // 4.  配置 html: DefinePlugin
    new webpack.DefinePlugin({
      BASE_URL: '"./"',
    }),
    new VueLoaderPlugin(),
  ],
};
